﻿using Com.AAS.Toolbox.DataMirror.Server.Handler;
using Com.AAS.Toolbox.DataMirror.Server.Handler.Impl;

namespace Com.AAS.Toolbox.DataMirror.Server
{
    class FileSystemFilterHandlerImpl : BaseHandlerImpl, FileSystemFilterHandler
    {

        private string[] fixeds = new string[0];
        private bool reverse = true;

        #region FileSystemFilterHandler 成员

        public bool DoFilter(MonitorEvent e)
        {
            if (e.HasEntityData)
            {
                if (e.EntityData.EntityType == EntityType.FILE)
                {
                    if (!reverse)
                    {
                        // 正向过滤
                        foreach (string endTag in fixeds)
                            if (e.EntityData.OriginalName.ToUpper().EndsWith(endTag.ToUpper()))
                                return true;
                    }
                    else
                    {
                        // 反向过滤
                        int isFindMatch = 0;
                        foreach (string endTag in fixeds)
                        {
                            if (e.EntityData.OriginalName.ToUpper().EndsWith(endTag.ToUpper()))
                                isFindMatch += 1;
                        }
                        // 每有找到匹配的文件则过滤调
                        if (isFindMatch == 0)
                            return true;
                    }
                }
            }
            return false;
        }


        public void SetFileFilterEndFixed(string[] fiexds, bool reverse)
        {
            this.fixeds = fiexds;
            this.reverse = reverse;
            if( this.fixeds == null)
                fixeds = new string[0];
            
        }

        #endregion
    }
}
